package com.qingfeng.provide.service;

import com.qingfeng.core.aop.exception.HttpException;
import com.qingfeng.core.dao.qingfeng.*;
import com.qingfeng.core.entity.qingfeng.Coupons;
import com.qingfeng.core.entity.qingfeng.Order;
import com.qingfeng.core.entity.qingfeng.Refund;
import com.qingfeng.core.entity.qingfeng.Task;
import com.qingfeng.core.mapper.qingfeng.OrderMapper;
import com.qingfeng.core.mapper.qingfeng.TaskMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @description:
 * @author:LWY
 * @date: 2017/11/19 14:41
 * @modified:
 */
@Service
public class TaskService {
    @Autowired
    TaskDao taskDao;
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    OrderDao orderDao;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    CouponsDao couponsDao;
    @Autowired
    FileDao fileDao;
    @Autowired
    RefundDao refundDao;

    /**
     * @param pay_way 支付方式（1：余额支付、2：支付宝、3：微信）
     */
    @Transactional
    public Map<String, Object> release(Task task, Integer pay_way) {
        if (task.getReward().intValue() < 1) {
            throw new HttpException("赏金低于1元,不能发布任务");
        }
        //生成任务
        taskDao.release(task);
        //把优惠券设置成已经使用状态
        if (StringUtils.isNotEmpty(task.getCoupons_id())) {
            Coupons coupons = couponsDao.use(task.getCoupons_id());
        }
        if (Objects.equals(pay_way, "1")) {
            //余额支付
            //todo
        } else if (Objects.equals(pay_way, "2")) {
            //支付宝支付
            //todo

        } else if (Objects.equals(pay_way, "3")) {
            //微信支付
            //todo

        }
        //生成订单
        BigDecimal amount = new BigDecimal(0);
        Order order = orderDao.create(1, task.getId(), amount, pay_way);
        Map<String, Object> map = new HashMap<>();
        map.put("task_id", task.getId());
        map.put("order_id", order.getId());
        map.put("pay_url", "");
        map.put("pay_url", "");
        return map;
    }

    /**
     * 获取任务详情
     *
     * @param id 任务id
     */
    public Map<String, Object> get(String id) {
        Map<String, Object> map = new HashMap<>();
        Task task = taskDao.byId(id);
        map.put("task", task);
        //获取讨论信息
        return null;
    }


    /**
     * 退款
     *
     * @param user_id     用户id
     * @param id          任务id
     * @param order_id    订单id
     * @param goods_state 退款时状态（1：未收到货、2：已经收到货）
     * @param rease       退款原因
     * @param instruction 退款说明
     * @param files       退款凭证
     */
    @Transactional
    public void refund(String user_id, String id, String order_id, Integer goods_state, String rease, String instruction, List<MultipartFile> files) {
        Task task = taskDao.byId(id);
        if (task.getState() != 6) {
            throw new HttpException("不是已接单状态的任务不能进行退款操作");
        }
        Order order = orderDao.byId(order_id);
        if (order.getState() != 2) {
            throw new HttpException("不是已付款状态的订单不能进行退款操作");
        }
        task.setState(7);//设置任务状态为退款中
        taskMapper.updateByPrimaryKeySelective(task);
        order.setState(3);//设置订单状态为退款中
        orderMapper.updateByPrimaryKeySelective(order);
        Refund refund = refundDao.insert(order_id, goods_state, rease, instruction);
        for (MultipartFile file : files) {
            String contentType = file.getContentType();
            String fileName = file.getOriginalFilename();
            //todo 上传文件
            String url = "";
            fileDao.save(2, 2, "", user_id, refund.getId(), url);
        }

    }
}
